layui.use(['layer', 'form', 'table', 'admin', 'ax'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;

    /**
     * 系统管理--消息管理
     */
    var ${g.entityName} = {
        tableId: "${g.lowerEntityName}Table"    //表格id
    };

    /**
     * 初始化表格的列
     */
    ${g.entityName}.initColumn = function () {
        return [[
            {type: 'checkbox'},
            <%for(field in t.fields){%>
            {field: '${field.name}', title: '${field.chinaName}', sort: true},
            <%}%>
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 200}
        ]];
    };

    /**
     * 点击查询按钮
     */
    ${g.entityName}.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(${g.entityName}.tableId, {where: queryData});
    };

    /**
     * 弹出添加${g.name}
     */
    ${g.entityName}.openAdd${g.entityName} = function () {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加${g.name}',
            content: Feng.ctxPath + '/${strutil.toLowerCase(g.entityName)}/goto_add',
            end: function () {
                admin.getTempData('formOk') && table.reload(${g.entityName}.tableId);
            }
        });
    };

    /**
     * 点击编辑${g.name}
     *
     * @param data 点击按钮时候的行数据
     */
    ${g.entityName}.onEdit${g.entityName} = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '${g.name}详情',
            content: Feng.ctxPath + '/${strutil.toLowerCase(g.entityName)}/goto_update/' + data.${g.idName},
            end: function () {
                admin.getTempData('formOk') && table.reload(${g.entityName}.tableId);
            }
        });
    };

    /**
     * 点击删除${g.name}
     *
     * @param data 点击按钮时候的行数据
     */
    ${g.entityName}.onDelete${g.entityName} = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/${strutil.toLowerCase(g.entityName)}/delete",
            function (data) {
                Feng.success("删除成功!");
                table.reload(${g.entityName}.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("${g.idName}", data.${g.idName});
            ajax.start();
        };
        Feng.confirm("是否删除${g.name} " + data.${g.idName} + "?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + ${g.entityName}.tableId,
        url: Feng.ctxPath + '/${strutil.toLowerCase(g.entityName)}/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: ${g.entityName}.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        ${g.entityName}.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        ${g.entityName}.openAdd${g.entityName}();
    });

    // 工具条点击事件
    table.on('tool(' + ${g.entityName}.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            ${g.entityName}.onEdit${g.entityName}(data);
        } else if (layEvent === 'delete') {
            ${g.entityName}.onDelete${g.entityName}(data);
        }
    });
});
